﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace LitTools
{
    public partial class NumberUtility
    {
        /// <summary>
        /// 取小数后x位
        /// 不会自动补零
        /// </summary>
        /// <param name="value"></param>
        /// <param name="place"></param>
        /// <returns></returns>
        public static float RoundFloatPlaces(float value, int place)
        {
            float num = Mathf.Pow(10, place);
            value = (int)(value * num);
            value /= num;
            return value;
        }

        /// <summary>
        /// 获取小数后x位字符串
        /// 可以自动补零
        /// </summary>
        /// <param name="value"></param>
        /// <param name="place"></param>
        /// <returns></returns>
        public static string RoundFloatPlacesString(float value,int place)
        {
            int count = GetFloatPlaces(value);
            
            if(count<place)
            {
                string result = value.ToString();
                for (int i = 0; i < place-count; i++)
                {
                    result += "0";
                }
                return result;
            }
            else
            {
                return RoundFloatPlaces(value, place).ToString();
            }
        }

        /// <summary>
        /// 取小数后x位
        /// 不会自动补零
        /// </summary>
        /// <param name="value"></param>
        /// <param name="place"></param>
        /// <returns></returns>
        public static double RoundDoublePlaces(double value, int place)
        {
            double num = Mathf.Pow(10, place);
            value = (int)(value * num);
            value /= num;
            return value;
        }

        /// <summary>
        /// 获取小数位数
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int GetFloatPlaces(float value)
        {
            string str = value.ToString();
            if (!str.Contains('.')) return 0;
            string[] parts = str.Split('.');
            return parts[1].Length;
        }

        /// <summary>
        /// 获取小数位数
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int GetDoublePlaces(double value)
        {
            string str = value.ToString();
            if (!str.Contains('.')) return 0;
            string[] parts = str.Split('.');
            return parts[1].Length;
        }
    }
}